//
//  FontPickerViewController.m
//  FontPicker
//
//  Created by easystudio on 11-10-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FontPickerViewController.h"

@implementation FontPickerViewController
@synthesize textLabel;
@synthesize font;
@synthesize color;
@synthesize textFontSize;

/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {	
 }
 */




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	self.color = [NSArray arrayWithObjects:[UIColor redColor],
				  [UIColor blueColor],
				  [UIColor greenColor],
				  [UIColor yellowColor],
				  [UIColor brownColor],
				  [UIColor purpleColor],
				  [UIColor darkGrayColor],
				  [UIColor grayColor],
				  [UIColor orangeColor],
				  nil];
	self.font=[UIFont familyNames];
	
	self.textFontSize=[NSArray arrayWithObjects:@"10", @"15", 
					   @"20", @"25", 
					   @"30", @"35",
					   @"40", @"45", nil];
	
	
}



/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.textLabel=nil;
	self.font=nil;
	self.color=nil;
	self.textFontSize=nil;
}


- (void)dealloc {
	[textLabel release];
	[font release];
	[color release];
	[textFontSize release];
    [super dealloc];
}

#pragma mark -
#pragma mark Picker Data Source And Delegate Methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	if (0==component) 
	{
		return [self.font count];
	}
	else if(1==component)
	{
		return [self.color count];
	}
	else 
	{
		return [self.textFontSize count];
	}
	
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	if (0==component) 
	{
		return 180;
	}
	else if(1==component)
	{
		return 90;
	}
	else 
	{
		return 50;
	}
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
	return 50.0f;
}

-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
	NSLog(@"view:%@", view);
	CGFloat width=[self pickerView:pickerView widthForComponent:component];
	CGFloat height=[self pickerView:pickerView rowHeightForComponent:component];
	
	UIView* myView=[[[UIView alloc] init]  autorelease];
	myView.frame=CGRectMake(0, 0, width, height-10.0f);
	UILabel* label=[[UILabel alloc] init];
	label.frame=myView.frame;
	label.tag=200;
	[myView addSubview:label];
	
	if(0==component)
	{
		label.text=(NSString*)[self.font objectAtIndex:row];
		
	}
	else if(1==component)
	{
		label.backgroundColor=(UIColor*)[self.color objectAtIndex:row];
		
	}
	else
	{
		label.text=(NSString*)[self.textFontSize objectAtIndex:row];
	}
	
	
	
	[label release];
	NSLog(@"myView:%@", myView);
	return myView;
	
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	int row0=[pickerView selectedRowInComponent:0];
	int row1=[pickerView selectedRowInComponent:1];
	int row2=[pickerView selectedRowInComponent:2];
	
	UIView* view0=[pickerView viewForRow:row0 forComponent:0];
	UIView* view1=[pickerView viewForRow:row1 forComponent:1];
	UIView* view2=[pickerView viewForRow:row2 forComponent:2];
	
	UILabel* label0=(UILabel*)[view0 viewWithTag:200];
	UILabel* label1=(UILabel*)[view1 viewWithTag:200];
	UILabel* label2=(UILabel*)[view2 viewWithTag:200];
	
	textLabel.font=[UIFont fontWithName:label0.text size:[label2.text floatValue]];
	textLabel.textColor=label1.backgroundColor;
}

@end
